package com.lwp.demo.config;

import com.fasterxml.jackson.core.filter.TokenFilter;
import com.lwp.demo.filter.ServerLoginFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.DispatcherType;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableCaching
public class MainConfig {
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
    @Bean
    FilterRegistrationBean<ServerLoginFilter> tokenFilter() {
        FilterRegistrationBean<ServerLoginFilter> filterReg = new FilterRegistrationBean<>(new ServerLoginFilter());
        //优先级
        filterReg.setOrder(70);
        filterReg.setDispatcherTypes(DispatcherType.REQUEST);
        //匹配路径
        List<String> urlPatterns = new ArrayList<>();
//        urlPatterns.add("/*");
        filterReg.addUrlPatterns("/server/*");
        filterReg.addInitParameter("exclusions","/main/*");
//        filterReg.setUrlPatterns(urlPatterns);
        return filterReg;
    }
}
